package com.mradio.app.web;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mradio.app.model.NewMessageModel;
import com.mradio.app.model.validator.NewMessageModelValidator;

@RequestMapping("/contact")
@Controller
public class ContactController {

	private final Logger logger = LoggerFactory.getLogger(ContactController.class);

	@Resource
	private MailSender mailSender;

	@Autowired
	private NewMessageModelValidator newMessageValidator;

	/**
	 * Initializes the form validator.
	 * 
	 * @param binder {@link WebDataBinder}
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.setValidator(newMessageValidator);
	}

	/**
	 * Handles the /contact request. Displays the form to create a new message.
	 * 
	 * @param model {@link ModelMap}
	 * @return the location to be displayed
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String showForm(ModelMap model) {
		return "contact";
	}

	/**
	 * Handles the form submit request.
	 * 
	 * @param formModel the form model
	 * @param bindingResult the validation result
	 * @param model {@link ModelMap}
	 * @param servletRequest {@link MultipartHttpServletRequest}
	 * @return location to be displayed
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(@Valid NewMessageModel formModel,
			BindingResult bindingResult, ModelMap model) {
		if (bindingResult.hasErrors()) {
			return "contact";
		}
		SimpleMailMessage mailMessage = new SimpleMailMessage();

		mailMessage.setTo("mradio.contact@gmail.com");
		String senderAddress = formModel.getSenderEmail();
		mailMessage.setFrom(senderAddress);
		mailMessage.setSubject(formModel.getSubject());
		mailMessage.setText(prepareMailText(formModel));

		try {
			mailSender.send(mailMessage);
		} catch (MailException e) {
			logger.error("Mail sending failed", e);
			return "error";
		}
		return "thankYou";
	}
	
	private String prepareMailText(NewMessageModel model) {
		return "Sender name: " + model.getSenderName() + "\r\n"
				+ "Sender email: " + model.getSenderEmail() + "\r\n"
				+ "Message: " + "\r\n" + model.getMessage();
	}

	/**
	 * Returns the form model.
	 * 
	 * @return {@link NewMessageModel}
	 */
	@ModelAttribute("newMessageModel")
	public NewMessageModel getFormModel() {
		NewMessageModel model = new NewMessageModel();
		logger.info("NewMessageModel initialized.");
		return model;
	}

}
